var LtBlock = drawList.add("assets/gfx/Blocks/LtBlock.png");
var MdBlock = drawList.add("assets/gfx/Blocks/MdBlock.png");
var HvBlock = drawList.add("assets/gfx/Blocks/HvBlock.png");


var block = {
    x: 0,
    y: 300,
    drawID: LtBlock, //Checks this to see what type of block it is.
    falling: false,
    beginFall: function ()  { this.falling = true; },
    isFalling: function()   { return this.falling;}
}

var blockWidth  = 32;
var MAP_WIDTH   = 10;
var MAP_HEIGHT  = 11;


var map = {

    //=====VARIABLES
    gridList: [],
    gridSize: 0,



    //=====CORE FUNCTIONS
    initialize: function () {
        var newBlock;
        for (var iy = 0; iy < MAP_HEIGHT; iy++) {
            for (var ix = 0; ix < MAP_WIDTH; ix++) {

                newBlock = new cloneObject(block);
                newBlock.x = 50 + ix * 32;
                newBlock.y = 300 + iy * 32;
                newBlock.drawID = this.genRandBlock();
                this.gridList[this.gridSize] = newBlock;
                this.gridSize++;

            } //End Ix
        } //End Iy

    }, //End initialize

    update: function (Player) {



        for (var i = 0; i < this.gridSize; i++) {

            //move the Entire map down and 
            // this.gridList[i].y += 0.25;                                 //Move the entre map downwards

            if (this.gridList[i].y > 600) this.gridList[i].beginFall();
            if (this.gridList[i].falling) this.gridList[i].y++;
            if (Player.checkCollide(this.gridList[i]))
                this.gridList[i].beginFall();


            //Determine the behaviors of the seperate blocks.
            if (this.gridList[i] && !this.gridList[i].isFalling()) {
                switch (this.gridList[i].drawID) {
                    case LtBlock: if (this.hasBlockAbove(i)) continue;
                    case MdBlock: if (this.hasBlockToSide) continue;
                    case HvBlock: if (this.hasBlockBelow(i)) continue;
                        this.gridList[i].beginFall();
                        break;
                } //End Switch
            } //End if
        } //End for

        //Looking Down in a list should be the same as adding a width.

    },

    //=====UNIQUE FUNCTIONS
    genRandBlock: function () {
        switch (Math.floor(Math.random() * 3)) {
            case 0: return LtBlock;
            case 1: return MdBlock;
            case 2: return HvBlock;
        }
    }, //End genRandBlock

    hasBlockAbove: function (element) {

        if (element - MAP_WIDTH < 0)
            return false;
        if (this.gridList[element].y > this.gridList[element - MAP_WIDTH].y + blockWidth)
            return false;
        return true;
    }, //End hasBlockAbove

    hasBlockBelow: function (element) {

        if (!this.gridList[element + MAP_WIDTH]) return false;
        if (this.gridList[element].y + blockWidth < this.gridList[element + MAP_WIDTH].y)
            return false;
        return true;
    }, //End hasBlockBelow

    hasBlockToSide: function (element) {

        if (element % MAP_WIDTH == 0) return false;
        if (element + 1 % MAP_WIDTH == 0) return false;

        if (this.gridList[element].y + blockWidth < this.gridList[element - 1].y)
            return false;
        if (this.gridList[element].y + blockWidth < this.gridList[element + 1].y)
            return false;
        return true;
    } //End hasBlockToSide
}//End Map

map.initialize();


/*

*In a table, you can check the element below the current one by adding the width to it.


*/